﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using BlogPlat.Model.Abstract;
using System.Collections;

namespace BlogPlat.Model.Repositories
{
    public class CategoryRepository : IRepository<Category>, IFriendlyUrl<Category>
    {
        private Table<Category> _categories;

        public CategoryRepository(string connString)
        {
            _categories = (new DataContext(connString)).GetTable<Category>();
        }

        #region Interface IRepository<I> Implementation
        public void Add(Category newEntity)
        {
            _categories.InsertOnSubmit(newEntity);
        }

        public void Delete(int id)
        {
            Category categoryToDelete = _categories.Where(c => c.CategoryID == id).SingleOrDefault();
            if(categoryToDelete != null)
                _categories.DeleteOnSubmit(categoryToDelete);
        }

        public ICollection<Category> List()
        {
            return _categories.ToList();
        }
        
        public void Save()
        {
            _categories.Context.SubmitChanges();
        }

        public Category Get(int id)
        {
            return _categories.Where(c => c.CategoryID == id).SingleOrDefault();
        }

        public void Update(Category entityToUpdate)
        {
            if (entityToUpdate.CategoryID == 0)
                return;
            else if (_categories.GetOriginalEntityState(entityToUpdate) == null)
            {
                _categories.Attach(entityToUpdate);
                _categories.Context.Refresh(RefreshMode.KeepCurrentValues, entityToUpdate);
            }
        }
        #endregion
      
        public List<Category> GetChildren(int categoryID)
        {
            return _categories.Where(c => c.ParentID == categoryID).ToList();
        }

        public void LoadRecursive(List<Category> categories, ref List<CategoryItem> dropDownItems, ref int depth)
        {                        
            foreach (Category category in categories)
            {
                if (!category.ParentID.HasValue)
                {
                    dropDownItems.Add(new CategoryItem { Text = category.CategoryName, Value = category.CategoryID.ToString() });
                    ++depth;
                    LoadRecursive(GetChildren(category.CategoryID), ref dropDownItems, ref depth);
                }
                else
                {
                    string tabs = GetTabForTree(depth);
                    dropDownItems.Add(new CategoryItem { Text = tabs + category.CategoryName, Value = category.CategoryID.ToString() });

                    List<Category> children = GetChildren(category.CategoryID);
                    if (children.Count > 0)
                    {
                        ++depth;
                        LoadRecursive(children, ref dropDownItems, ref depth);
                        --depth;
                    }                     
                }                
            }
        }

        private string GetTabForTree(int depth)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < depth; i++)
                sb.Append("----");

            return sb.ToString();
        }

        #region Interface IFriendlyUrl Implementation
        public Category GetByFriendlyUrl(string friendlyUrl)
        {
            if (!string.IsNullOrEmpty(friendlyUrl))
                return _categories.Where(c => c.FriendlyUrl == friendlyUrl).SingleOrDefault();
            else return null;
        }
        #endregion        
    }
}
